package com.leo;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Orientation;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollBar;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
 * @ClassName ScrollBarDemo
 * @Description
 * @Author wangss
 * @date 2020.05.26 20:27
 * @Version 1.0
 */
public class ScrollBarDemo extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        AnchorPane anchorPane = new AnchorPane();
        anchorPane.setStyle("-fx-background-color: #e7eeea");
        Scene scene = new Scene(anchorPane);

        VBox hBox = new VBox();
        for (int i = 0; i <= 10; i++) {
            hBox.getChildren().add(new Button("button" + i));
        }

        ScrollBar scrollBar = new ScrollBar();
        scrollBar.setOrientation(Orientation.VERTICAL);

        AnchorPane.setLeftAnchor(scrollBar, 100d);
        anchorPane.getChildren().addAll(scrollBar, hBox);

        scrollBar.setVisibleAmount(50d);
        scrollBar.setValue(20d);

        primaryStage.setScene(scene);
        primaryStage.setTitle("spinner");
        primaryStage.setWidth(625d);
        primaryStage.setHeight(500d);
        primaryStage.setResizable(false);
        primaryStage.show();

        scrollBar.valueProperty().addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
                System.out.println(newValue);
                hBox.setLayoutY(newValue.doubleValue());
            }
        });
    }
}
